package com.glory.gloryquickui.adapter

import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.SingleChooseOptionsBean
import com.glory.gloryquickui.utils.FloatingWindowsUtils

class CheckboxSelectAdapter(
    data: List<SingleChooseOptionsBean.Option?>?,
    mCallback: FloatingWindowsUtils.PopTextAndPositionCallback,
    disable : Boolean,
    border : Boolean) : BaseQuickAdapter<SingleChooseOptionsBean.Option?, BaseViewHolder>(
    R.layout.item_checkbox,
    data as MutableList<SingleChooseOptionsBean.Option?>?
) {

    private val mDisable : Boolean = disable
    private val mBorder : Boolean = border
    private var mCallback: FloatingWindowsUtils.PopTextAndPositionCallback? = mCallback
    override fun convert(helper: BaseViewHolder, item: SingleChooseOptionsBean.Option?) {
        helper.setText(R.id.tvSelectName, item?.label)
        val cbSelect = helper.getView<CheckBox>(R.id.cbSelect)
        val llCheckLayout = helper.getView<LinearLayout>(R.id.llCheckLayout)
        cbSelect.isEnabled = !mDisable
        //设置单个禁用
        cbSelect.isEnabled = item!!.disabled
        //设置选中不可用
        cbSelect.isChecked = item!!.isSelect
        //设置边框
        if (mBorder){
            llCheckLayout.setBackgroundResource(
                if (cbSelect.isChecked) R.drawable.shape_blue_line_white_fill_corner12_select
                else R.drawable.shape_gray_line_white_fill_corner12_select)
        }
        cbSelect.setOnCheckedChangeListener { _: CompoundButton, isChecked: Boolean ->
            item!!.isSelect = isChecked
            mCallback?.onSuccess(item?.value.toString(), helper.adapterPosition)
            if (mBorder){
                notifyItemChanged(helper.adapterPosition)
            }
        }
    }
}